package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.yifei.Cmsmq;
import com.example.exception.CustomException;
import com.example.mapper.CmsmqMapper;
import com.example.service.CmsmqService;
import com.example.untils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-02-27
 */
@Service
public class CmsmqServiceImpl extends ServiceImpl<CmsmqMapper, Cmsmq> implements CmsmqService {

    @Autowired
    private CmsmqMapper mapper;

    /**
     * @param type 单别
     * @param tableName 表名
     * @return
     */
    @Override
    public String getIdByType(String type, String tableName) {
        if(StringUtils.isEmpty(type)) {
            throw new CustomException("500", "单别为空");
        }
        if(StringUtils.isEmpty(tableName)) {
            throw new CustomException("500", "表名为空");
        }
        Cmsmq cmsmq = mapper.selectById(type);
        if(cmsmq == null) {
            throw new CustomException("500", "单别:" + type + "在CMSMQ中不存在");
        }
        tableName = tableName.toUpperCase();
//        int yearSize = cmsmq.getMq005().intValue();
//        int last = cmsmq.getMq006().intValue();
//        System.out.println(yearSize);
        String lastTwo = tableName.substring(tableName.length() - 2).toUpperCase();

        String string = "";
        // 单号固定为11位
        if(cmsmq.getMq004().equals("1")) {  // 日编
            string = String.format("SELECT ISNULL(CAST(CAST(MAX(%s002) AS NUMERIC(11))+1 AS CHAR(11)) ," +
                    "CONVERT(CHAR(8),GETDATE(),112)+'001') AS 单号 FROM %s WHERE %s002 " +
                    "BETWEEN CONVERT(CHAR(8),GETDATE(),112)+'001' AND CONVERT(CHAR(8),GETDATE(),112)+'999' " +
                    "AND %s001 = '%s' AND LEN(%s002) = 11", lastTwo, tableName, lastTwo, lastTwo, type, lastTwo);
        }else if(cmsmq.getMq004().equals("2")) {  // 月编
            string = String.format("SELECT ISNULL(CAST(CAST(MAX(%s002) AS NUMERIC(11))+1 AS CHAR(11)) ," +
                    "CONVERT(CHAR(6),GETDATE(),112)+'00001') AS 单号 FROM %s WHERE %s002 " +
                    "BETWEEN CONVERT(CHAR(6),GETDATE(),112)+'00001' AND CONVERT(CHAR(6),GETDATE(),112)+'99999' " +
                    "AND %s001 = '%s' AND LEN(%s002) = 11", lastTwo, tableName, lastTwo, lastTwo, type, lastTwo);
        }else if(cmsmq.getMq004().equals("3")) {
            string = String.format("SELECT ISNULL(CAST(CAST(MAX(%s002) AS NUMERIC(11))+1 AS CHAR(11)) ," +
                    "'00000000001') AS 单号 FROM %s WHERE %s002 " +
                    "BETWEEN '00000000001' AND '99999999999' " +
                    "AND %s001 = '%s' AND LEN(%s002) = 11", lastTwo, tableName, lastTwo, lastTwo, type, lastTwo);
        }else{
            throw new CustomException("500", "手动编号无法自动生成");
        }
        return mapper.getIdByType(string);
    }

    @Override
    public String getIdByType2(String type, String tableName) {
//        Cmsmq cmsmq = mapper.selectById(type);
//        if(cmsmq == null) {
//            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
//        }
//        tableName = tableName.toUpperCase();
//        String lastTwo = tableName.substring(tableName.length() - 2).toUpperCase();
//
//        String string = "";
//        // 单号固定为11位
//        if(cmsmq.getMq004().equals("1")) {  // 日编
//            string = String.format("SELECT ISNULL(CAST(CAST(MAX(%s002) AS NUMERIC(11))+1 AS CHAR(11)) ," +
//                    "CONVERT(CHAR(8),GETDATE(),112)+'001') AS 单号 FROM %s WHERE %s002 " +
//                    "BETWEEN CONVERT(CHAR(8),GETDATE(),112)+'001' AND CONVERT(CHAR(8),GETDATE(),112)+'999' " +
//                    "AND %s001 = '%s'", lastTwo, tableName, lastTwo, lastTwo, type);
//        }else if(cmsmq.getMq004().equals("2")) {  // 月编
//            string = String.format("SELECT ISNULL(CAST(CAST(MAX(%s002) AS NUMERIC(11))+1 AS CHAR(11)) ," +
//                    "CONVERT(CHAR(6),GETDATE(),112)+'00001') AS 单号 FROM %s WHERE %s002 " +
//                    "BETWEEN CONVERT(CHAR(6),GETDATE(),112)+'00001' AND CONVERT(CHAR(6),GETDATE(),112)+'99999' " +
//                    "AND %s001 = '%s'", lastTwo, tableName, lastTwo, lastTwo, type);
//        }else if(cmsmq.getMq004().equals("3")) {
//            string = String.format("SELECT ISNULL(CAST(CAST(MAX(%s002) AS NUMERIC(11))+1 AS CHAR(11)) ," +
//                    "'00000000001') AS 单号 FROM %s WHERE %s002 " +
//                    "BETWEEN '00000000001' AND '99999999999' " +
//                    "AND %s001 = '%s'", lastTwo, tableName, lastTwo, lastTwo, type);
//        }
//        Integer id = mapper.getIdByType2(string);
        return null;
    }
}
